<!--
 * @Author: 李天惊 uf_lee@163.com
 * @Date: 2022-11-10 16:37:30
 * @LastEditors: 李小晴 2689279066@qq.com
 * @LastEditTime: 2023-11-14 11:58:34
 * @FilePath: \grazlink20\uc_sys\src\views\Shop\Withdrawal\Index.vue
 * Copyright (c) 2022 by 李天惊 uf_lee@163.com, All Rights Reserved. 
-->
<script setup lang="ts">
import { reactive, ref } from 'vue'
import { ContentWrap } from '@/components/ContentWrap'
import { Search } from '@/components/Search'
import { ElButton } from 'element-plus'
import { Table } from '@/components/Table'
// import { adminCsWithdrawalListApi } from '@/api/cs/withdrawal'
import { useTable } from '@/hooks/web/useTableTest'
import { CrudSchema, useCrudSchemas } from '@/hooks/web/useCrudSchemas'
// 获取字典
import { useCache } from '@/hooks/web/useCache'
import { dbCsWithdrawalListApi, dbCsWithdrawalexportApi } from '@/api/forms'
import { zcPastureListApi } from '@/api/fa/user'
const { wsCache } = useCache()
const dict: any = wsCache.get('dict')
// 字典通过value获取label
const getDictLabel = (type, value) => {
  let data = dict[type].find((item) => {
    return item.value == value
  })
  return data ? data.label : ''
}

const { register, tableObject, methods } = useTable<
  {
    total: number
    list: any[]
  },
  any
>({
  getListApi: dbCsWithdrawalListApi,
  response: {
    data: 'data',
    count: 'count'
  }
})

const { setSearchParams } = methods

const crudSchemas = reactive<CrudSchema[]>([
  {
    field: 'withdrawal_id',
    width: 80,
    label: '主键'
  },
  {
    field: 'withdrawal_sn',
    width: 180,
    label: '提现单编号'
  },
  {
    field: 'org_id',
    label: '组织名称',
    table: {
      show: false
    },
    search: {
      component: 'DialogRefer',
      componentProps: {
        getListApi: zcPastureListApi,
        response: {
          count: 'count',
          data: 'data'
        },
        title: '选择组织（双击选择）',
        nameField: 'org_name',
        relNameField: 'org_name',
        valueField: 'org_id',
        relValueField: 'org_id',
        crudSchemas: [
          {
            field: 'org_name',
            label: '组织名称',
            fixed: 'left',
            search: {
              show: true
            }
          }
        ]
      },
      show: true
    },
    form: {
      component: 'DialogRefer',
      componentProps: {
        getListApi: zcPastureListApi,
        response: {
          count: 'count',
          data: 'data'
        },
        title: '选择组织（双击选择）',
        nameField: 'org_name',
        relNameField: 'org_name',
        valueField: 'org_id',
        relValueField: 'org_id',
        crudSchemas: [
          {
            field: 'org_name',
            label: '组织名称',
            fixed: 'left',
            search: {
              show: true
            }
          }
        ]
      }
    }
  },
  {
    field: 'org_name',
    width: 180,
    label: '组织名称'
  },
  // {
  //   field: 'user_name',
  //   label: '用户昵称'
  // },
  {
    field: 'amount',
    width: 100,
    label: '金额'
  },
  {
    field: 'bank_account',
    label: '账户',
    width: 150,
    search: {
      show: true
    }
  },
  {
    field: 'bank_name',
    width: 180,
    label: '银行'
  },
  {
    field: 'status',
    label: '状态',
    formatter: (_: Recordable, __: TableColumn, cellValue: number | string) => {
      return cellValue != '0' ? getDictLabel('cs_withdrawal_status', cellValue) : ' '
    },
    search: {
      component: 'Select',
      componentProps: {
        options: dict.cs_withdrawal_status
      },
      show: true
    }
  },
  {
    field: 'user_name',
    width: 100,
    label: '创建人',
    search: {
      show: true
    }
  },
  {
    field: 'create_time',
    label: '创建时间',
    width: 170,
    form: {
      show: true,
      component: 'DatePicker',
      componentProps: {
        disabled: true,
        type: 'datetime',
        valueFormat: 'YYYY-MM-DD HH:mm:ss'
      }
    },
    search: {
      show: true,
      component: 'DatePicker',
      componentProps: {
        'is-range': true,
        'unlink-panels': true,
        rangeSeparator: 'To',
        type: 'daterange',
        valueFormat: 'YYYY-MM-DD HH:mm:ss'
      }
    }
  }
])

const { allSchemas } = useCrudSchemas(crudSchemas)

const exportClick = async () => {
  dialogTableVisible.value = true
  let aaa: any = tableObject.paramsObj.params
  console.log(aaa, tableObject.count)
  let res: any = await dbCsWithdrawalListApi({
    ...tableObject.paramsObj.params
  })
    .catch(() => {})
    .finally(() => {})
  if (res) {
    FormsNumber.value = Math.ceil(res.count / 10000)
  }
}

const FormsNumber = ref(0)
const dialogTableVisible = ref(false)

const Export = async (index) => {
  let aaa: any = tableObject.paramsObj.params
  console.log(aaa, index)
  let { data }: any = await dbCsWithdrawalexportApi({
    request: {
      ...tableObject.paramsObj.params,
      pageNo: index,
      pageSize: '10000'
    },
    layout_data: {
      file_name: '提现单列表',
      column_cell_start: 'A2',
      column_data: [
        {
          title: '序号',
          width: 5,
          cell_type: 'key'
        },
        {
          title: '组织名称',
          width: 20,
          cell_type: 'string',
          give_type: 'variable',
          give_value: 'org_name'
        },
        {
          title: '出款单编号',
          width: 20,
          cell_type: 'string',
          give_type: 'variable',
          give_value: 'withdrawal_sn'
        },
        {
          title: '金额',
          width: 10,
          cell_type: 'number',
          decimal: 2,
          give_type: 'variable',
          give_value: 'amount',
          sum: true
        },
        {
          title: '账户',
          width: 10,
          cell_type: 'string',
          give_type: 'variable',
          give_value: 'bank_account'
        },
        {
          title: '银行',
          width: 10,
          cell_type: 'string',
          give_type: 'variable',
          give_value: 'bank_name'
        },
        {
          title: '状态',
          width: 10,
          cell_type: 'dict',
          dict_type: 'cs_income_status',
          give_type: 'variable',
          give_value: 'status'
        },
        {
          title: '创建人',
          width: 20,
          cell_type: 'string',
          give_type: 'variable',
          give_value: 'user_name'
        },
        {
          title: '创建时间',
          width: 20,
          cell_type: 'string',
          give_type: 'variable',
          give_value: 'create_time'
        }
      ],
      custom: [
        {
          cell_type: 'string',
          cell: 'A1:I1',
          give_type: 'format',
          give_value: '提现单列表',
          horizontal: 'center',
          border: true,
          size: '16'
        }
      ]
    }
  })
    .catch(() => {})
    .finally(() => {
      // saveLoading.value = false
    })
  if (data) {
    window.open(data.url)

    console.log(data, Math.ceil(data.count / 10000))
    // FormsNumber.value = Math.ceil(data.count / 10000)
  }
}
</script>

<template>
  <ContentWrap>
    <Search :schema="allSchemas.searchSchema" @search="setSearchParams" @reset="setSearchParams" />
    <div class="mb-10px w-[100%] flex justify-end" v-show="tableObject.paramsObj.params">
      <ElButton type="primary" @click="exportClick()"> 导出EXCEL </ElButton>
    </div>

    <Table
      v-show="tableObject.paramsObj.params"
      v-model:pageSize="tableObject.pageSize"
      v-model:currentPage="tableObject.currentPage"
      :columns="allSchemas.tableColumns"
      :data="tableObject.tableList"
      :loading="tableObject.loading"
      :border="true"
      :pagination="{
        total: tableObject.count
      }"
      @register="register"
      :selection="false"
    />
  </ContentWrap>

  <el-dialog v-model="dialogTableVisible" title="提现主档导出详情">
    <div style="margin: 20px" v-for="(item, index) in FormsNumber" :key="index">
      <span style="margin: 20px">提现主档 - {{ item }}</span>
      <ElButton type="primary" @click="Export(item)">导出</ElButton>
    </div>
  </el-dialog>
</template>
